// pages/login/index.js
var app = getApp();
var CommonUtils = app.CommonUtils;
const Toptips = require('../../dist/toptips/index');
var Keys = require('../../assets/data/keys.js');

Page(Object.assign({}, Toptips, {

  /**
   * 页面的初始数据
   */
  data: {
    userName: '', //用户名
    userPassword: '', //密码
    login_id: '',  //token
    response: '',  //存取网络请求返回的数据
    open_id: '',
  },

  /**
   * 用户名输入
   */
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },

  /**
   * 用户密码输入
   */
  userPasswordInput: function (e) {
    this.setData({
      userPassword: e.detail.value
    })
  },

  /**
   * 登陆按钮
   */
  btnLogin: function (e) {
    if (this.data.userName.length == 0 || this.data.userPassword.length == 0) {
      this.showZanTopTips("请完善登陆信息");
      return
    }
    var that = this;
    var username = that.data.userName, password = that.data.userPassword, openid = that.data.open_id;
    wx.request({
      url: Keys.data.url_Login,
      data: {
        DataType: "Login",
        usercode: username,
        password: password,
        wxid: openid,
      },
      success: function (res) {
        console.log(res)
        if (res.data.isok == "true") {
          wx.setStorage({
            key: Keys.data.login_id,
            data: res.data.data.HP_ID,
            success: function () {
              console.log(res.data.data.HP_ID)
              app.login_id = res.data.data.HP_ID;
              CommonUtils.showToastWithImg("登陆成功", "1")
              setTimeout(function () {
                wx.navigateBack({ delta: 1 })
              }, 1500)
            },
            fail: function () {
              CommonUtils.showToastWithImg("请重新登陆", "1")
            }
          })
        } else {
          that.showZanTopTips(res.data.info);
        }
      }
    })
  },

  /**
   * 注册账号按钮
   */
  btnRegister: function (e) {
    wx.navigateTo({
      url: '../register/index',
    })
  },

  /**
   * 忘记密码按钮
   */
  btnForgetPassword: function (e) {
    wx.navigateTo({
      url: '../find-password/index',
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.userName != null && options.userPassword != null) {
      this.setData({
        userName: options.userName,
        userPassword: options.userPassword,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
}))